package com.ruoyi.examation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.examation.domain.Exam;
import com.ruoyi.examation.domain.ExamClassExam;
import com.ruoyi.examation.vo.ExamVo;
import com.ruoyi.examation.vo.ExamClassExamVo;
import com.ruoyi.examation.mapper.ExamMapper;
import com.ruoyi.examation.mapper.ExamClassExamMapper;
import com.ruoyi.examation.service.IExamService;
import com.ruoyi.examation.service.IExamClassExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 考试信息Service业务层处理
 *
 * @author Tellsea
 * @date 2025-09-28
 */
@Service
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IExamService {

    @Autowired
    private IExamClassExamService examClassExamService;

    @Autowired
    private ExamClassExamMapper examClassExamMapper;

    @Override
    public TableDataInfo<ExamVo> queryList(ExamVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<ExamVo> queryAll(ExamVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public ExamVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public TableDataInfo<ExamClassExamVo> queryExamClassList(Long examId, ExamClassExamVo entity) {
        entity.setExamId(examId);
        return examClassExamService.queryExamClassList(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addExamClasses(Long examId, List<Long> classIds) {
        if (classIds == null || classIds.isEmpty()) {
            return 0;
        }

        int successCount = 0;
        for (Long classId : classIds) {
            // 检查关系是否已存在
            QueryWrapper<ExamClassExam> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("exam_id", examId).eq("class_id", classId);
            
            if (examClassExamMapper.selectCount(queryWrapper) > 0) {
                // 关系已存在，跳过
                continue;
            }
            
            // 创建新的考试班次关系
            ExamClassExam examClassExam = new ExamClassExam();
            examClassExam.setExamId(examId);
            examClassExam.setClassId(classId);
            
            int result = examClassExamMapper.insert(examClassExam);
            if (result > 0) {
                successCount++;
            }
        }

        return successCount;
    }

    @Override
    public int removeExamClass(Long examId, Long classId) {
        QueryWrapper<ExamClassExam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("exam_id", examId).eq("class_id", classId);
        return examClassExamMapper.delete(queryWrapper);
    }

    @Override
    public TableDataInfo<ExamVo> queryMyClassExamList(Long userId, ExamVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryMyClassExamList(PageUtils.buildPage(), userId, entity));
    }
}
